<?php

use App\User;
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class UserTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        User::insert([
            [
            'name'  =>  'wamy10000',
            'email' =>  'wamy10000@126.com',
            'password'  =>  bcrypt('7986546'),
            ],
            [
            'name'  =>  'asd1',
            'email' =>  'asd1@126.com',
            'password'  =>  bcrypt('7986546'),
            ],
            [
            'name'  =>  'asd2',
            'email' =>  'asd2@126.com',
            'password'  =>  bcrypt('7986546'),
            ],
            [
            'name'  =>  'asd3',
            'email' =>  'asd3@126.com',
            'password'  =>  bcrypt('7986546'),
            ]
        ]);

        $owner = new \App\Role();
        $owner->name         = 'owner';
        $owner->display_name = '普通用户'; // optional
        $owner->description  = '普通用户只有浏览权限'; // optional
        $owner->save();

        $editor = new \App\Role();
        $editor->name         = 'editor';
        $editor->display_name = '编辑'; // optional
        $editor->description  = '编辑有浏览和修改的权限'; // optional
        $editor->save();

        $admin = new \App\Role();
        $admin->name          = 'admin';
        $admin->display_name  = '系统管理员';
        $admin->description   = '系统管理员拥有浏览，修改，添加，删除的权限';
        $admin->save();

        $god = new \App\Role();
        $god->name          = 'god';
        $god->display_name  = '超级管理员';
        $god->description   = '系统管理员拥有最高权限';
        $god->save();


        $view = new \App\Permission();
        $view->name         = 'view';
        $view->display_name = '查看'; // optional
        // Allow a user to...
        $view->description  = '仅限查看权限'; // optional
        $view->save();

        $editUser = new \App\Permission();
        $editUser->name         = 'edit';
        $editUser->display_name = '编辑'; // optional
        // Allow a user to...
        $editUser->description  = '有修改的权限'; // optional
        $editUser->save();

        $add = new \App\Permission();
        $add->name         = 'add';
        $add->display_name = '添加'; // optional
        // Allow a user to...
        $add->description  = '添加权限'; // optional
        $add->save();

        $del = new \App\Permission();
        $del->name         = 'del';
        $del->display_name = '删除'; // optional
        // Allow a user to...
        $del->description  = '删除权限'; // optional
        $del->save();

        $owner->attachPermission($view);
        // equivalent to $admin->perms()->sync(array($createPost->id));

        $editor->attachPermissions(array($view, $editUser));
        // equivalent to $owner->perms()->sync(array($createPost->id, $editUser->id));
        $admin->attachPermissions(array($view,$editUser,$add,$del));

        $god->attachPermissions(array($view,$editUser,$add,$del));

        $user = User::where('email', '=', 'wamy10000@126.com')->first();
        $user1 = User::where('email', '=', 'asd1@126.com')->first();
        $user2 = User::where('email', '=', 'asd2@126.com')->first();
        $user3 = User::where('email', '=', 'asd3@126.com')->first();
        //$user->attachRole($admin);
        $user->roles()->attach( $god->id );
        $user1->roles()->attach( $admin->id );
        $user2->roles()->attach( $editor->id );
        $user3->roles()->attach( $owner->id );

    }
}